#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include<mypushbutton.h>
#include<chooselevelscene.h>
#include<QTimer>
#include<QSoundEffect>
MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);
    //配置主场景
    //设置固定大小
    setFixedSize(320,588);

    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));

    //设置标题
    setWindowTitle("能为我改成金色吗");

    //退出按钮的实现
    connect(ui->actiontc,&QAction::triggered,[=](){
        this->close();
    });

    //准备开始按钮的音效

    QSoundEffect * startSound = new QSoundEffect(this);
    startSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

//    QSoundEffect startSound;
//    startSound.setLoopCount(QSoundEffect::Infinite);
//    startSound.setVolume(0.25f);
//    tartSound.play();
    //开始按钮
    MypushButton * startBtn = new MypushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width() *0.5 - startBtn->width()*0.5,this->height()*0.7    );
    //实例化选择关卡场景
    choosescene = new ChooseLevelScene;

    //            监听选择关卡的返回按钮的信号
    connect(choosescene,&ChooseLevelScene::chooseSceneBack,this,[=](){
        this->setGeometry(choosescene->geometry());
        choosescene->hide();
        this->show();
    });

    connect(startBtn,&MypushButton::clicked,[=](){
        qDebug() << "被点击了开始";
        startSound->play();
        //做弹起特效
        startBtn->zoom1();
        startBtn->zoom2();

//        延时操作 500毫秒后
        QTimer::singleShot(800,this,[=]{
            //设置choose场景的位置
            choosescene->setGeometry(this->geometry());
            //进入选择关卡场景  自身隐藏 显示选择关卡场景
            this->hide();
            choosescene->show();
        });
    });
}
void MainScene::paintEvent(QPaintEvent *event)
{
//    创建画家 指定绘图设备
    QPainter painter(this);
//    创建QPixmap对象
    QPixmap pix;
//    加载图片
    pix.load(":/res/PlayLevelSceneBg.png");
//    绘制背景图
    painter.drawPixmap(0,0,this->width(),this->height(),pix);


//    加载标题
    pix.load(":/res/Title.png");
//    缩放图片
    //pix = pix.scaled(pix.width() * 0.5,height() * 0.5);
//    绘制标题
    painter.drawPixmap(10,30,pix.width() * 0.5,pix.height() * 0.5,pix);
}
MainScene::~MainScene()
{
    delete ui;
}

